package com.sljy.hzk.util;

import android.util.Log;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * Created  on 2018/6/27 0027
 *
 * @author 唐开阔
 * @describe 时间相关工具类
 */
public class TimeUtils {
    private static String mYear;
    private static String mMonth;
    private static String mDay;
    private static String mWay;

    /**
     * 获取当前日期
     *
     * @return
     */
    public static Date getNowDate() {
        return Calendar.getInstance().getTime();
    }

    /**
     * date  to  string
     * @param dateDate
     * @return
     */
    public static String dateToStr(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    public static String dateToStrMd (Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("hh:mm");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    public static String dateToStr(Date dateDate, String replace) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dateDate);
        String s1 = dateString.substring(0,4)+"\n";
        String s2 = dateString.substring(5,10);
        dateString = s1+s2;
        return dateString.replace("-",replace);
    }


    /**
     * string   to  date
     */
    public static Date strToDate(String string) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = formatter.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    //2个日期差多少天
    public static long dateDiff(String startTime, String endTime) {
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        long day = 0;
        try {
            // 获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime()
                    - sd.parse(startTime).getTime();
            day = diff / nd;// 计算差多少天
            long hour = diff % nd / nh;// 计算差多少小时
            long min = diff % nd % nh / nm;// 计算差多少分钟
            long sec = diff % nd % nh % nm / ns;// 计算差多少秒
            // 输出结果
            System.out.println("时间相差：" + day + "天" + hour + "小时" + min
                    + "分钟" + sec + "秒。");
            return day;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;

    }

    /**
     * 比较2个日期大小
     * */
    public static int compare_date(String DATE1, String DATE2) {
        if(DATE2.equals(""))
            return 1;
       //DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date dt1 = df.parse(DATE1);
            Date dt2 = df.parse(DATE2);
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("dt1 在dt2后");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("dt1在dt2前");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }


    /**
     * 传入年月日获取时间戳
     *
     * @param year
     * @param month
     * @param dayOfMonth
     * @return
     */
    public static Date getDateByNyr(int year, int month, int dayOfMonth) {
        Calendar cal = Calendar.getInstance();
        cal.set(year, month, dayOfMonth);
        return cal.getTime();
    }

    /**
     * 获取前一天
     *
     * @param date
     * @return
     */
    public static Date getBeforeDay(Date date) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        int day = cl.get(Calendar.DATE);
        cl.set(Calendar.DATE, day - 1);
        return cl.getTime();
    }

    /**
     * 获取后一天
     * @param date
     * @return
     */
    public static Date getTomorrowDay(Date date) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        int day = cl.get(Calendar.DATE);
        cl.set(Calendar.DATE, day + 1);
        return cl.getTime();
    }

    public static Date getTomorrowDay(Date date, int d) {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        int day = cl.get(Calendar.DATE);
        cl.set(Calendar.DATE, day + d);
        return cl.getTime();
    }


    public static List<Date> getTomorrowWeek(Date date){
        List<Date> list = new ArrayList<Date>();
        for(int i = 1; i < 8; i++){
            list.add(getTomorrowDay(date, i));
        }
        return list;
    }

    public static List<Date> getBeforeWeek(Date date){
        List<Date> list = new ArrayList<Date>();
        for(int i = 1; i < 8; i++){
            list.add(getTomorrowDay(date, -i));
        }
        return list;
    }


    /**
     * 获取后一周
     * @param date
     * @return
     */
  public static Date GetNextWeekDay(Date date){
      Calendar cl = Calendar.getInstance();
      cl.setTime(date);
      int day = cl.get(Calendar.DATE);
      cl.set(Calendar.DATE, day+7);
      return cl.getTime();
  }
    /**
     * 获取前一周
     * @param date
     * @return
     */
    public static Date GetLastWeekDay(Date date){
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        int day = cl.get(Calendar.DATE);
        cl.set(Calendar.DATE, day-7);
        return cl.getTime();
    }

    /**
     * 获取当前年月日字符串
     *
     * @return
     */
    public static String getNowNyr() {
        return dateToStr(new Date());
    }

    /**
     * 获取当前星期几
     *
     * @return
     */
    public static String getWeekDay() {
        final Calendar c = Calendar.getInstance();
        c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
        mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "天";
        } else if ("2".equals(mWay)) {
            mWay = "一";
        } else if ("3".equals(mWay)) {
            mWay = "二";
        } else if ("4".equals(mWay)) {
            mWay = "三";
        } else if ("5".equals(mWay)) {
            mWay = "四";
        } else if ("6".equals(mWay)) {
            mWay = "五";
        } else if ("7".equals(mWay)) {
            mWay = "六";
        }
        return "星期" + mWay;
    }

    /**
     * 判断两个日期是否为同一个页面
     * @param dayOne
     * @param dayTwo
     * @return
     */
    public static boolean isOnemMonth(String dayOne,String dayTwo){
        Date dateOne = strToDateTwo(dayOne);
        Date dateTwo = strToDateTwo(dayTwo);
        Calendar c1 = Calendar.getInstance();
        c1.setTime(dateOne);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(dateTwo);
        return c1.get(Calendar.YEAR) ==  c2.get(Calendar.YEAR) &&c1.get(Calendar.MONTH) ==  c2.get(Calendar.MONTH);
    }
    public static Date strToDateTwo(String string) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = formatter.parse(string);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 截取年月
     * @return
     */
    public static String getMonth(String str){
        Date date = strToDateTwo(str);
        SimpleDateFormat formatter = new SimpleDateFormat("MM月");
        String dateString = formatter.format(date);
        Date nowDate = new Date();
        String nowM = formatter.format(nowDate);
        if (dateString.equals(nowM)){
            return "本月";
        }
        return dateString;
    }

    /**
     * 把小时换算成H 或 天
     * */
    public static String dateState(String time){
        if(time.equals(""))
            return "0h";
        String str = time.substring(0,time.length()-1);
        Integer d = Integer.parseInt(str);
        if(d > 24){
            return  Math.round(d/24) +"天";
        }
        return d+"h";
    }

    /**
     * 紧急程度
     * */
    public static Integer dateStateString (String time){
        if(time.equals(""))
            return 2;
        String str = time.substring(0,time.length()-1);
        Integer d = Integer.parseInt(str);
        if(d > 24)
            return 0;//不紧急
        else if(d > 10)
            return 1;//一般
        return 2;//紧急
    }
}
